# server tools/config

import string
import sys

#serverid = "TestServer"
#dbtype = "mysql"
#net = "irc"

nodelist = []
net = "tcp"
pollint = 180
bpollint = 120
plugins = {}
bufplugins = {}
coll_interval = {}
debug = True
dbuser = "pyhmon"
dbpass = "de723605"
dbhost = "localhost"

def init () :
	global serverid
	global dbtype
	global net
	global nodelist
	global bpollint
	global pollint
	global plugins
	global bufplugins
	global coll_interval
	global debug
	global dbuser
	global dbhost
	global dbpass

	try :
		f = open ("etc/pyhmon_server.conf")
		cont = list (string.split (f.read (), "\n"))
		f.close ()
	except :
		sys.exit ("Can't open file: etc/pyhmon_server.conf")

	for c in cont :
		if len (c) > 0 :
			if not c[0] == "#" :
				param = list (string.split (c, "="))
				if param[0] == "id" :
					serverid = param[1]
				elif param[0] == "net" :
					net = param[1]
				elif param[0] == "db" :
					db = param[1]
				elif param[0] == "nodes" :
					nodes = list (string.split (param[1], ","))
				elif param[0] == "poll_interval" :
					pollint = int (param[1])
				elif param[0] == "bpoll_interval" :
					bpollint = int (param[1])
				elif param[0] == "debug" :
					if param[1] == "yes" :
						debug = True
					elif param[1] == "no" :
						debug = False
	
	for node in nodes :
		coll_interval[node] = {}
		plugins[node] = []
		plugins[node] = []
		try :
			f = open ("etc/nodes/%s" % node)
			cont = list (string.split (f.read(), "\n"))
			f.close ()
		except :
			sys.exit ("Can't open file: etc/nodes/%s" % node)
		for c in cont :
			if len (c) > 0 :
				if not c[0] == "#" :
					param = list (string.split (c, "="))
					if param[0] == "plugins" :
						plugins[node] = list (string.split (param[1], ","))
					elif param[0] == "bplugins" :
						bufplugins[node] = list (string.split (param[1], ","))
					elif param[0] == "address" :
						addr = param[1]
					elif param[0] == "port" :
						port = int (param[1])
					else :
						par0 = string.split(param[0], "_")
						if par0[1] == "collectint" and par0[0] in \
						bufplugins[node] :
							coll_interval[node][par0[0]] = int (param[1])
		try :
			nodelist.append ([node, addr, port])
		except :
			sys.exit ("Node %s is not well configured." % node)
	
	try :
		f = open ("etc/db/%s.conf" % db)
		cont = list (string.split (f.read (), "\n"))
		f.close ()
	except :
		sys.exit ("Cannot open file: etc/db/%s.conf" % db)
	
	for c in cont :
		if len (c) > 0 :
			if not c[0] == "#" :
				param = string.split (c, "=")
				if param[0] == "host" :
					dbhost = param[1]
				elif param[0] == "user" :
					dbuser = param[1]
				elif param[0] == "pass" :
					dbpass = param[1]

def getDBUser () :
	global dbuser
	return dbuser

def getDBHost () :
	global dbhost
	return dbhost

def getDBPass () :
	global dbpass
	return dbpass
			
def getServerID () :
	global serverid
	return serverid

def getDBType () :
	global dbtype
	return dbtype

def getNetBackend () :
	global net
	return net

def getNodeList () :
	global nodelist
	return nodelist

def getPollInterval () :
	global pollint
	return pollint

def getBPollInterval () :
	global bpollint
	return bpollint

def getSimplePlugins (nodename) :
	global plugins
	return plugins[nodename]

def getBufPlugins (nodename) :
	global bufplugins
	return bufplugins[nodename]

def getCollInterval (nodename, pluginname) :
	global coll_interval
	return coll_interval[nodename][pluginname]

def getDebug () :
	global debug
	return debug
